import React, { Component } from 'react'
import './index.css'

export default class Item extends Component {
  constructor(props) {
    super(props)
    this.state = { mouse: false }
  }

  // 鼠标移入移出事件
  handleMouse = (flag) => {
    return () => {
      this.setState({ mouse: flag })
    }
  }

  // 勾选框check事件
  handleCheck = (id) => {
    return (e) => {
      console.log(e.target.checked)
      this.props.updateTodo(id, e.target.checked)
    }
  }

  // 删除事件
  handleDelete = (id) => {
    if(window.confirm('你确定要删除吗?')) {
      this.props.deleteTodo(id)
    }
  }

  render() {
    const { id, name, done } = this.props
    const { mouse } = this.state
    return (
      <li
        style={{ backgroundColor: mouse ? '#eee' : '' }}
        onMouseEnter={this.handleMouse(true)}
        onMouseLeave={this.handleMouse(false)}
      >
        <label>
          <input
            type="checkbox"
            onChange={this.handleCheck(id)}
            checked={done}
          />
          <span>{name}</span>
        </label>
        <button
          onClick={() => this.handleDelete(id)}
          className="btn btn-danger"
          style={{ display: mouse ? 'block' : 'none' }}
        >
          删除
        </button>
      </li>
    )
  }
}
